package com.smart.module.car.util;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.smart.common.util.DateUtils;
import com.smart.module.car.entity.CarParkManage;
import com.smart.module.car.entity.CarParkingRecord;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;

/**
 * 计算费用
 */
public class CostUtils {

    /**
     * 计算
     * @param record
     * @param carParkManage
     * @return
     */
   /* public static BigDecimal calculate(CarParkingRecord record, CarParkManage carParkManage){
        //计算停车时间
        long minutes = DateUtil.between(record.getGmtInto(),
                DateUtils.getTimestamp(), DateUnit.MINUTE);
        minutes = minutes - carParkManage.getFreeTime();
        if(minutes>0){
            //根据进场时间\出场时间   确定在哪个时间段来获取停车费
            // 获取进场时间和出场时间的小时部分
            int entryHour = DateUtil.hour(record.getGmtInto(), true);
            int outHour = DateUtil.hour(new Date(), true);

            Integer startTime1 = Integer.valueOf(carParkManage.getStartTime1().split(":")[0]);
            Integer endTime1 = Integer.valueOf(carParkManage.getEndTime1().split(":")[0]);
            Integer startTime2 = Integer.valueOf(carParkManage.getStartTime2().split(":")[0]);
            Integer endTime2 = Integer.valueOf(carParkManage.getEndTime2().split(":")[0]);

            BigDecimal count =
                    NumberUtil.div(new BigDecimal(minutes),carParkManage.getTimeUnit());
            count = NumberUtil.round(count,0);


            BigDecimal cost ;
            //白天收费段
            if(entryHour>startTime1 && entryHour<endTime1){
                cost= NumberUtil.mul(count,carParkManage.getUnitCost());
            }else{
                cost= NumberUtil.mul(count,carParkManage.getUnitCost2());
            }


            cost = NumberUtil.round(cost,0);
            if(cost.compareTo(carParkManage.getMaxMoney())>1){
                return carParkManage.getMaxMoney();
            }
            return cost;
        }else{
            return new BigDecimal(BigInteger.ZERO);
        }
    }*/
    public static BigDecimal calculate(CarParkingRecord record, CarParkManage carParkManage) {
        // 计算停车总分钟数
        long totalMinutes = DateUtil.between(record.getGmtInto(), DateUtils.getTimestamp(), DateUnit.MINUTE);
        totalMinutes -= carParkManage.getFreeTime(); // 减去免费时长

        if (totalMinutes <= 0) {
            return BigDecimal.ZERO; // 如果停车时间小于或等于免费时长，费用为0
        }

        // 定义时间段和对应的费用
        int startTime1 = Integer.parseInt(carParkManage.getStartTime1().split(":")[0]);   // 白天开始时间 6:00
        int endTime1 = Integer.parseInt(carParkManage.getEndTime1().split(":")[0]);   // 白天开始时间 6:00
        int startTime2 = Integer.parseInt(carParkManage.getStartTime2().split(":")[0]);   // 白天开始时间 6:00
        int endTime2 = Integer.parseInt(carParkManage.getEndTime2().split(":")[0]);   // 白天开始时间 6:00


        BigDecimal dayRate = carParkManage.getUnitCost();  // 白天收费 2元/小时
        BigDecimal nightRate = carParkManage.getUnitCost2(); // 夜间收费 1元/小时

        // 获取停车的开始和结束时间
        Date entryTime = record.getGmtInto();  // 15:00
        Date exitTime = DateUtils.getTimestamp(); // 21:40
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(entryTime);


        BigDecimal totalCost = BigDecimal.ZERO;

        // 分别处理白天和夜晚的收费
        while (totalMinutes > 0) {
            int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
            BigDecimal unitCost = BigDecimal.ZERO;

            // 计算当前时间段的剩余时间
            int remainingMinutesInPeriod = 0;

            if (currentHour >= startTime1 && currentHour < endTime1) {
                // 当前时间段为白天时间段
                remainingMinutesInPeriod = ((endTime1 - currentHour) * 60) - calendar.get(Calendar.MINUTE);
                unitCost = dayRate;
            } else if (currentHour >= startTime2 || currentHour < endTime2) {
                // 当前时间段为夜间时间段
                if (currentHour >= startTime2) {
                    remainingMinutesInPeriod = ((24 - currentHour + endTime2) * 60) - calendar.get(Calendar.MINUTE);
                } else {
                    remainingMinutesInPeriod = (endTime2 - currentHour) * 60 - calendar.get(Calendar.MINUTE);
                }
                unitCost = nightRate;
            } else {
                // 当前时间段为非收费时间段，直接跳过
                calendar.add(Calendar.HOUR_OF_DAY, 1);
                continue;
            }

            // 计算当前时间段内的收费时间
            int chargeableMinutes = (int) Math.min(totalMinutes, remainingMinutesInPeriod);

            // 计算费用
            BigDecimal count = NumberUtil.div(new BigDecimal(chargeableMinutes), BigDecimal.valueOf(60));
            BigDecimal cost = count.multiply(unitCost);

            // 累加总费用
            totalCost = totalCost.add(cost);

            // 减去已计算的分钟数
            totalMinutes -= chargeableMinutes;

            // 更新时间到下一个时间段
            calendar.add(Calendar.MINUTE, chargeableMinutes);
        }

        // 确保费用不会超过最大费用（如果有设置）
        if (totalCost.compareTo(carParkManage.getMaxMoney()) > 0) {
            return carParkManage.getMaxMoney();
        }

        return NumberUtil.round(totalCost, 2); // 返回总费用
    }

}
